package dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class RentDao {
    public static boolean rentDao(String sName){
        PreparedStatement preparedStatement = null;
        Connection connection = null;
        ResultSet resultSet = null;
        try {
            connection = DBConnection.getConnection();
            preparedStatement = connection.prepareStatement("select * from discs where d_name=?");
            preparedStatement.setString(1, sName);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()&&resultSet.getInt(5)==1) {
                int anInt = resultSet.getInt(4)+1;
                String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                preparedStatement=connection.prepareStatement("update discs set d_date=?,d_key=?,d_ZT=? where d_name=?");
                preparedStatement.setDate(1, Date.valueOf(date));
                preparedStatement.setInt(2,anInt);
                preparedStatement.setBoolean(3,false);
                preparedStatement.setString(4,sName);
                preparedStatement.executeUpdate();
                return true;
            }
    }catch (Exception e){
        return false;}finally {
            DBConnection.release(connection,preparedStatement,resultSet);
        }
    return false;}
}
